﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.ProjectArtist
{
    public class ProjectArtistDao : BaseDao, IProjectAstistDao
    {
        /// <summary>
        /// 删除应征者
        /// </summary>
        /// <param name="projectId">需求id</param>
        /// <param name="ownerId">用户id</param>
        /// <returns></returns>
        public bool Delete(int projectId, int ownerId)
        {
            return db.Deleteable<Models.ProjectArtists>()
                .Where(it => it.ProjectID == projectId && it.OwnerID == ownerId)
                .ExecuteCommandHasChange();
        }

        public List<ProjectArtists> GetListByOwnerId(int ownerId, int pageIndex = 1, int pageSize = 12)
        {
            return db.Queryable<Models.ProjectArtists>()
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .Mapper(it => it.ResumeInfo, it => it.OwnerID, it => it.ResumeInfo.OwnerID)
                .Mapper(it => it.Artworks, it => it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
                .Where(it => it.OwnerID == ownerId)
                .ToPageList(pageIndex, pageSize);
        }

        /// <summary>
        /// 根据作品id，返回应征者
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<ProjectArtists> GetListByProjectId(int projectId, int pageIndex = 1, int pageSize = 12)
        {
            return db.Queryable<Models.ProjectArtists>()
                .Mapper(it=>it.Owner,it=>it.OwnerID,it=>it.Owner.UserID)
                .Mapper(it=>it.ResumeInfo,it=>it.OwnerID,it=>it.ResumeInfo.OwnerID)
                .Mapper(it=>it.Artworks,it=>it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
                .Where(it => it.ProjectID == projectId)
                .ToPageList(pageIndex, pageSize);
        }

        public ProjectArtists GetProjectAstistByProjectIdAndOwnerId(int projectid, int ownerId)
        {
            return db.Queryable<Models.ProjectArtists>()
               .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
               .Mapper(it => it.ResumeInfo, it => it.OwnerID, it => it.ResumeInfo.OwnerID)
               .Mapper(it => it.Artworks, it => it.Artworks.First().OwnerID)
               .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
               .Where(it => it.OwnerID == ownerId && it.ProjectID == projectid)
               .First();
        }

        public int GetTotal()
        {
            return db.Queryable<Models.ProjectArtists>().Count();
        }

        public bool Insert(Models.ProjectArtists projectArtist)
        {
            return db.Insertable<Models.ProjectArtists>(projectArtist)
                .ExecuteCommandIdentityIntoEntity();
        }
    }
}
